package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.hjq.base.BaseDialog;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.SingleClick;
import com.qiantu.phone.app.AppApplication;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 提示对话框
 */
public final class ConfirmCancelHintDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        @Nullable
        private OnListener mListener;
        private OnRightTopListener mRightTopListener;

        private final ImageView hintIcon;
        private final TextView tvHint;
        private final TextView tvContent;
        private final TextView btnLeft;
        private final TextView btnRight;
        private final TextView btnRightTop;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.confirm_cancel_hint_dialog);
//            setCancelable(false);
//            setCanceledOnTouchOutside(false);
            setWidth(getResources().getDimensionPixelSize(R.dimen.dp_343));
            hintIcon = findViewById(R.id.icon_hint);
            tvHint = findViewById(R.id.tv_hint);
            tvContent = findViewById(R.id.tv_content);
            btnLeft = findViewById(R.id.btn_left);
            btnRight = findViewById(R.id.btn_right);
            btnRightTop = findViewById(R.id.btn_right_top);
            setOnClickListener(btnLeft, btnRight, btnRightTop);
        }

        public Builder setHintIcon(int res) {
            hintIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(res));
            return this;
        }

        public Builder setHint(String text) {
            tvHint.setText(text);
            return this;
        }

        public Builder setContent(String text) {
            tvContent.setText(text);
            return this;
        }

        public Builder setContent(@StringRes int res) {
            tvContent.setText(res);
            return this;
        }

        public Builder setLeftText(@StringRes int res) {
            btnLeft.setText(res);
            return this;
        }

        public Builder setLeftText(String res) {
            btnLeft.setText(res);
            return this;
        }

        public Builder setRightText(@StringRes int res) {
            btnRight.setText(res);
            return this;
        }

        public Builder setRightEnable(Boolean enable) {
            btnRight.setEnabled(enable);
            return this;
        }
        public Builder setRightText(String res) {
            btnRight.setText(res);
            return this;
        }

        public Builder setRightTopText(String res) {
            btnRightTop.setText(res);
            if(TextUtils.isEmpty(res)){
                btnRightTop.setVisibility(View.GONE);
            }else{
                btnRightTop.setVisibility(View.VISIBLE);
            }
            return this;
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        public Builder setRightTopListener(OnRightTopListener listener) {
            mRightTopListener = listener;
            return this;
        }

        @Override
        public BaseDialog create() {
            return super.create();
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_left:
                    dismiss();
                    if (mListener != null) {
                        mListener.onLeftClick(getDialog());
                    }
                    break;
                case R.id.btn_right:
                    dismiss();
                    if (mListener != null) {
                        mListener.onRightClick(getDialog());
                    }
                    break;
                case R.id.btn_right_top:
                    dismiss();
                    if(mRightTopListener != null){
                        mRightTopListener.onRightTopClick(getDialog());
                    }
                    break;
            }
        }

        public Builder setRightBtnBackground(int resId) {
            btnRight.setBackgroundResource(resId);
            return this;
        }
    }

    public interface OnListener {
        void onLeftClick(BaseDialog dialog);
        void onRightClick(BaseDialog dialog);
    }

    public interface OnRightTopListener {
        void onRightTopClick(BaseDialog dialog);
    }
}